﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class Clients_Control_News : System.Web.UI.UserControl
{
    private String strLiDiv = "<li> <a href='" + Constant.ROOT_PATH + "$link'>$text</a></li>";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {

            LoadDataLeft();
            
        }
    }

    private int Index(String str, String find) {
        int vt = -1;
        vt = str.IndexOf(find);
        return vt;
    }

    private void LoadDataLeft()
    {

        WebData data = new WebData();
        String strSQL = "select news.idNew,lang.titleNew,lang.bodyNew,lang.introNew,news.imageNew,news.isAuto"
            + ",news.addedDate from tbl_News_Lang lang,tbl_News news"
            + " where lang.idNew=news.idNew and idLang=1 and isTindacbiet=0 order by news.idNew desc";
        using (DamiWebDataContext db = new DamiWebDataContext())
        {
            var query = from p in db.tbl_News_Langs
                        where p.tbl_New.idNew == p.idNew
                        && p.idLang == 1 && p.tbl_New.isTindacbiet==false
                        orderby p.tbl_New.addedDate descending
                        select new
                        {
                            p.titleNew,
                            p.idNew,
                            p.tbl_New.imageNew,
                            p.tbl_New.addedDate,
                            p.bodyNew,
                            p.introNew,
                            p.tbl_New.isAuto
                        };

            
            //DataTable dt = data.tbl_Newss.GetDataTable(strSQL, CommandType.Text);
            List<News> list = new List<News>();
            int i = 0;
            String url = "";
            foreach (var c in query) 
            {
                News news = new News();
                news.idNew = c.idNew;
                String strFind = "";// c.introNew;
                int begin, end;
                if (c.isAuto == true)
                {
                    strFind = c.introNew;
                
                    begin = Index(strFind, "src=\"http://");
                    end = Index(strFind, ".jpg");
                    if (begin != -1 && end > begin + 3)
                    {
                        strFind = strFind.Substring(begin + 5, end - begin - 1);
                    }

                }
                else {
                    strFind = Constant.ROOT_PATH + c.imageNew.ToString().Trim().Replace("~/","");
                    strFind = strFind.Trim();
                }
                String strRece = c.introNew;
                begin = Index(strRece, "<a");
                end = Index(strRece, "</a>");
                Boolean bBool = false;
                if(end>begin){
                    strRece = strRece.Substring(begin, end+1);
                    bBool = true;
                }
                news.imageNew = strFind;
                if (bBool)
                {
                    news.introNew = c.introNew.Replace(strRece, "");
                }
                else
                    news.introNew ="<p>"+ c.introNew+"</p>";
                news.titleNew = c.titleNew;
                list.Add(news);
                i++;
                if (i == 10)
                    break;
            }
            this.grdLeft.DataSource = list;// query.Skip(0).Take(10);
            this.grdLeft.DataBind();
            
            url = "";
            i = 0;
            foreach (var c in query)
            {
              
              if(i>10)
                {
                    try
                    {
                        if (!string.IsNullOrEmpty(c.ToString()))
                        {
                            String str = String.Format("New/{0}/{1}", c.idNew.ToString(), c.titleNew.ToString().
                                Replace(" ", "-").Replace(".", "").Replace("/", "_").Replace(":", "").Replace("\"", ""));
                            url = url + strLiDiv.Replace("$text", c.titleNew).Replace("$link", str);
                        }
                    }
                    catch { }
                    if (i >= 24)
                        break;
                }
                i++;
            }

            ltlTintuc.Text = url;
            if (string.IsNullOrEmpty(url))
            {
                lbl.Visible = false;
            }
            else
            {
                lbl.Visible = true;
            }
        }
    }

}

